/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.yuecai.exception;

import com.yuecai.annotation.TableComment;

import javax.persistence.Table;

/**
 * @author Zheng Jie
 * @date 2018-11-23
 */
public class EntityExistException extends RuntimeException {

    /**
     * 给后台提示
     * @param clazz
     * @param field
     * @param val
     */
    public EntityExistException(Class clazz, String field, String val) {
        super(EntityExistException.generateMessage(clazz, field, val));
    }

    private static String generateMessage(Class entity, String field, String val) {
        Table annotation = (Table) entity.getAnnotation(Table.class);

        return annotation.name()+ " 中存在 " + field + " 为 "+ val + "的数据";
    }


    /**
     * 给用户提示
     * @param clazz
     * @param val
     */
    public EntityExistException(Class clazz, String val) {

        super(EntityExistException.generateCommentMessage(clazz, val));
    }

    private static String generateCommentMessage(Class entity, String val) {
        TableComment annotation = (TableComment) entity.getAnnotation(TableComment.class);
        return annotation.name()+ " 中存在 " + " 为 "+ val + "的数据";
    }



}